package com.zengdada.authority.utils.excel.export.style;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * @author 12614
 * @contact 1261438511@qq.com
 * @date 2020-06-07 22:39
 * @describe 帮助设置 字体
 */
public class MyFontUtil {
    public static Font setCellStyleFont(Workbook workbook, CellStyle cellStyle, MyFont font) {
        Font originalFont = workbook.getFontAt(cellStyle.getFontIndexAsInt());
        MyFont mNewFont = font;
        if (originalFont != null) {
            mNewFont = new MyFont(originalFont).merge(font);
        }

        Font newFont = null;

        int num = workbook.getNumberOfFontsAsInt();
        MyFont tmpFont;
        Font wbFont;
        for (int i = 0; i < num; i++) {
            wbFont = workbook.getFontAt(i);
            tmpFont = new MyFont(wbFont);
            if (tmpFont.equals(mNewFont)) {
                newFont = wbFont;
                break;
            }
        }

        if (newFont == null) {
            newFont = workbook.createFont();
            font.initFont(newFont);
        }
        cellStyle.setFont(newFont);
        return newFont;
    }
}
